/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 */

import { CommonConst } from '../const/common-const.js';
import { DistroFilterDimensionEnum } from '../enum/distro-filter-dimension-enum.js';

import { DistroFilterConfig, DistroFilterHandler } from './distro-filter-handler.js';

/**
 * distroFilter校验链入口
 */
export class DistroFilterValidateEntrance extends DistroFilterHandler {
  /**
   * @param sourceDistroFilterConfig
   * @param targetDistroFilterConfig
   */
  validateDistroFilterRequest(sourceDistroFilterConfig: DistroFilterConfig, targetDistroFilterConfig: DistroFilterConfig): void {
    const errorInfo = DistroFilterHandler.validateMultiTarget(this.projectModel) ? 'executable targets' : 'modules';
    const cause = 'The distroFilter settings are left empty' + ` in the entry ${errorInfo} in ${CommonConst.PROFILE_JSON5} or ${CommonConst.CONFIG_JSON}.`;
    const detail =
      'Select dimension: apiVersion, screenShape or screenWindow to configure and ' +
      'make sure the distroFilter settings can uniquely identify an entry module.';
    if (this.checkDistroFilterObjEmpty(sourceDistroFilterConfig, targetDistroFilterConfig)) {
      this.logger._buildError(cause)._detail(detail)._printErrorAndExit();
    } else if (this.nextDimensionHandler) {
      this.nextDimensionHandler.validateDistroFilterRequest(sourceDistroFilterConfig, targetDistroFilterConfig);
    }
  }

  getDimensionValue(distroFilterConfig: DistroFilterConfig): number[] | string[] | undefined {
    return undefined;
  }

  /**
   * 检查当前工程模块distroFilter对象是否配置
   * @param sourceDistroFilterConfig
   * @param targetDistroFilterConfig
   * @private
   */
  private checkDistroFilterObjEmpty(sourceDistroFilterConfig: DistroFilterConfig, targetDistroFilterConfig: DistroFilterConfig): boolean {
    const withoutApiVersion =
      this.checkDimensionConfigEmpty(sourceDistroFilterConfig, DistroFilterDimensionEnum.API_VERSION) &&
      this.checkDimensionConfigEmpty(targetDistroFilterConfig, DistroFilterDimensionEnum.API_VERSION);
    const withoutScreenShape =
      this.checkDimensionConfigEmpty(sourceDistroFilterConfig, DistroFilterDimensionEnum.SCREEN_SHAPE) &&
      this.checkDimensionConfigEmpty(targetDistroFilterConfig, DistroFilterDimensionEnum.SCREEN_SHAPE);
    const withoutScreenWindow =
      this.checkDimensionConfigEmpty(sourceDistroFilterConfig, DistroFilterDimensionEnum.SCREEN_WINDOW) &&
      this.checkDimensionConfigEmpty(targetDistroFilterConfig, DistroFilterDimensionEnum.SCREEN_WINDOW);
    return withoutApiVersion && withoutScreenShape && withoutScreenWindow;
  }
}
